package com.mo.security.domain;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;

import java.util.List;
import java.util.Objects;

/**
 * 模拟数据库，获取用户
 */
public class SimulateUserDB {
    // 密码：123456
    public static final String password = "$2a$10$pBF161DBY1y1ZI682V8uXOMeVeunLZg05/8TRYAWw8gyYMWibUxha";

    // 模拟用户集合
    public static final List<User> users = List.of(
            getAdmin(),
            getUser()
    );

    public static User getUserById(Integer id) {
        return users.stream().filter(user -> Objects.equals(user.getId(), id)).findFirst().orElse(null);
    }

    public static User getUserByUsername(String username) {
        return users.stream().filter(user -> Objects.equals(user.getUsername(), username)).findFirst().orElse(null);
    }

    // 模拟管理员
    private static User getAdmin() {
        String[] auths = {"sys:add", "sys:delete", "sys:edit", "sys:get", "ROLE_ADMIN"};
        List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(auths);
        return new User(1001, "admin", password, authorities);
    }

    // 模拟普通用户
    private static User getUser() {
        String[] auths = {"ROLE_USER", "sys:get"};
        List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(auths);
        return new User(2001, "user", password, authorities);
    }
}
